import configForm from './config';
import { updateValue } from '@/api/public';
import { createDiscreteApi } from 'naive-ui';
import { deepClone } from '@/utils';
import { ref } from 'vue';

export default (userInfo, modelRef) => {
    const formList = ref(deepClone(configForm));
    const showModal = ref(false);
    const { message } = createDiscreteApi(['message']);
    // 修改密码
    const changePassword = () => {
        console.log(`output->formList.value`, formList.value);
        showModal.value = true;
    };
    const validatePasswordSame = (rule, value) => {
        const data = modelRef.value.getFormData();
        return value === data.password;
    };
    const formRules = ref({
        password: [
            {
                required: true,
                message: '请输入密码',
                trigger: ['blur']
            }
        ],
        passwordConfirm: [
            {
                required: true,
                message: '请再次输入密码',
                trigger: ['blur']
            },
            {
                validator: validatePasswordSame,
                message: '两次密码输入不一致',
                trigger: ['blur']
            }
        ]
    });
    const onSave = data => {
        updateValue('T_USER', {
            _id: userInfo.value._id,
            password: data.password
        }).then(() => {
            message.success('操作成功');
            showModal.value = false;
        });
    };
    return {
        showModal,
        formList,
        formRules,
        changePassword,
        onSave
    };
};
